using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace FROG
{
    public static class App
    {
        public static string AppData
        {
            get
            {
                string appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.ProductName);
                if (!Directory.Exists(appData))
                    Directory.CreateDirectory(appData);
                return appData;
            }
        }

        #region Configuration

        private static Config _config;
        public static Config Config
        {
            get { return _config; }
            set { _config = value; }
        }

        public static void LoadConfig()
        {
            string filename = Path.Combine(AppData, "config.xml");
            LoadConfig(filename);
            //Config.CodeSnippets.Add(new CodeSnippet("sel", "select * from "));
        }

        public static void LoadConfig(string filename)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Config));
            using (StreamReader rd = new StreamReader(filename))
            {
                App.Config = xs.Deserialize(rd) as Config;
            }
        }

        public static void SaveConfig()
        {
            string filename = Path.Combine(AppData, "config.xml");
            SaveConfig(filename);
        }

        public static void SaveConfig(string filename)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Config));
            using (StreamWriter wr = new StreamWriter(filename))
            {
                xs.Serialize(wr, App.Config);
            }
        }

	    #endregion

        private static ObservableList<OpenConnection> _openConnections = new ObservableList<OpenConnection>();

        public static ObservableList<OpenConnection> OpenConnections
        {
            get { return _openConnections; }
            set { _openConnections = value; }
        }

        private static ObservableList<WorkSheet> _workSheets = new ObservableList<WorkSheet>();
        public static ObservableList<WorkSheet> WorkSheets
        {
            get { return _workSheets; }
            set { _workSheets = value; }
        }

        private static int _worksheetNumber = 1;
        public static int NextWorksheetNumber()
        {
            return _worksheetNumber++;
        }
    }
}
